#coding=utf-8
#author :sh

import time
from qiniu import Auth, put_file, etag, urlsafe_base64_encode
import qiniu.config
import os
import cv2
from threading import Timer
#需要填写你的 Access Key 和 Secret Key
access_key = '3AINPWZ_mRDs4ALejUh78ytFnttSTXUjrzctrkPm' #这里的密钥填上刚才我让你记住的密钥对
secret_key = 'rG1XXz6NioBOmr-Dgfe0b2a9R6amck-AFRJc4LPr' #这里的密钥填上刚才我让你记住的密钥对
time_interval = 1.0


def main():
	while(1):
		q = Auth(access_key, secret_key)#构建鉴权对象
		bucket_name = 'pi-monitor'      #上传空间
		key = '%s_%s_%s_%s_%s_%s.jpg'%(time.localtime()[0],time.localtime()[1],time.localtime()[2],time.localtime()[3],time.localtime()[4],time.localtime()[5])
		#上传到七牛后保存的文件名
		token = q.upload_token(bucket_name, key, 3600)#生成上传 Token，可以指定过期时间等
		print "running"
		ret, frame = cap.read()
		cv2.imwrite("current_photo.jpg",frame)
		time.sleep(1)
		localfile = 'current_photo.jpg'
		ret,info  = put_file(token, key, localfile)
		filename  = 'current_photo.jpg'
		if os.path.exists(filename):
			os.remove(filename)


if __name__=='__main__':
	cap = cv2.VideoCapture(0)
	print "hello"
	
	try:
		main()
	except KeyboardInterrupt:
		print "error"

